<!--
User: ${author}
Date: ${date}
view: ${table.comment!} 视图
-->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
         contentType="text/html;charset=utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head>
    <title>${table.comment!} 列表</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.4/css/layui.css"/>
</head>
<body style="height: 100%">
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" type="button" lay-event="add">添加数据
        </button>
    </div>
</script>
<div style="width:100%;padding: 0px 10px;margin-top:10px">

    <form class="layui-form layui-form-pane" action="">

<#if fieldList?exists>
    <#list fieldList?keys as key>
        <div class="layui-form-item">
            <#assign fieldLists = fieldList[key]>
            <#if fieldLists??>
                <#list fieldLists! as field>
                <label class="layui-form-label"
                       style="width: 150px"><#if field.comment!?length gt 0>${field.comment}<#else >${field.propertyName}</#if></label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="${field.propertyName}" id="${field.propertyName}">
                </div>
                </#list>
                <#if fieldList?size == (key?index + 1)>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" data-type="reload" lay-submit="" lay-filter="querySubmit">查询
                    </button>
                </div>
                </#if>
            </#if>
        </div>
    </#list>
</#if>
    </form>
</div>
<div style="width:100%;padding: 0px 10px;">
    <table class="layui-hide" id="listTable" lay-filter="listTable">
        <thead>
        <tr>
            <#list table.fields as field>
                <th lay-data="{field:'${field.propertyName}', minWidth: 150}"><#if field.comment!?length gt 0>${field.comment}<#else >${field.propertyName}</#if></th>
            </#list>
            <th lay-data="{field:'${table.primaryKeyName}', minWidth: 200,templet: '#titleTpl'}">操作</th>
        </tr>
        </thead>
    </table>
</div>
</body>
</html>
<script src="https://www.layuicdn.com/layui-v2.5.4/layui.js"></script>
<script id="titleTpl" type="text/html">
    <a href="javascript:;" title="查看" lay-event="select" tcid="{{d.${table.primaryKeyName}}}"><i
            class="layui-icon">&#xe615;</i>查看&nbsp;&nbsp;</a>
    <a href="javascript:;" title="修改" tcid="{{d.${table.primaryKeyName}}}" lay-event="update"><i class="layui-icon">&#xe642;</i>修改&nbsp;&nbsp;</a>
    <a href="javascript:;" title="删除" tcid="{{d.${table.primaryKeyName}}}" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</a>
</script>
<script>
    layui.use(["layer", "form", "table"], function () {
        let table = layui.table, $ = layui.jquery, layer = layui.layer, form = layui.form
                , path = "<%=basePath%>";

        table.init("listTable", {
            url: path + "/${table.capitalEntityName}/queryTableData"
            , toolbar: '#toolbarDemo'
            , sort: true
            , limit: 10
            , limits: [10, 20, 50, 80, 100]
            , page: true
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }, request: {
                pageName: 'pageIndex' //页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            id: "listReload"
        });

        // 查询
        form.on("submit(querySubmit)", function (data) {
            //执行重载
            table.reload('listReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: data.field
            });
        });

        // 弹窗
        let layerOpen = (title, url, height, width) => {
            if (height == null) {
                height = ($(window).height() - 50);
            }
            if (width == null) {
                width = ($(window).width() * 0.9);
            }
            layer.open({
                type: 2,
                area: [width + 'px', height + 'px'],
                fix: false, //不固定
                maxmin: true,
                shadeClose: true,
                shade: 0.4,
                title: title,
                content: url
            });
        };

        //头工具栏事件
        table.on('toolbar(listTable)', function (obj) {
            let checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    layerOpen("添加", path + "/${table.capitalEntityName}/goSaveView", null, null);
                    break;
            }
        });

        // 工具栏
        table.on('tool(listTable)', function (obj) {
            let obj_this = $(this);
            let id = obj_this.attr("tcid");
            switch (obj.event) {
                case "delete":
                    // 删除
                    layer.confirm("确认要删除吗?", {icon: 3}, function (indexOpen) {
                        let indexLoad;
                        $.ajax({
                            type: 'post',
                            dataType: 'json',
                            url: path + "/${table.capitalEntityName}/delete${table.entityName}ById/" + id,
                            traditional: true,
                            beforeSend: function () {
                                indexLoad = layer.msg('请稍候……', {
                                    icon: 16,
                                    shade: 0.01,
                                    shadeClose: false,
                                    time: 120000
                                });
                            },
                            success: function (res) {
                                if (res.status === 200) {
                                    layer.msg("删除成功.", {icon: 6});
                                    table.reload('listReload');
                                } else {
                                    layer.msg(res.msg, {icon: 5, shift: 6});
                                }
                            }, error: function (e) {
                                layer.msg('请求异常，请重试', {icon: 5, shift: 6});
                            },
                            complete: function () {
                                layer.close(indexLoad);
                            }
                        });
                    });
                    break;
                case "update":
                    // 修改
                    layerOpen("修改", path + "/${table.capitalEntityName}/goUpdateView?id="+id, null, null);
                    break;
                case "select":
                    // 查看
                    layerOpen("查看", path + "/${table.capitalEntityName}/goUpdateView?id="+id, null, null);
                    break;
            }
        });

    });
</script>
